package com.smooth.site.sx.pc.job.controller.yaoqinghan;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smooth.common.pc.interceptor.PcAccountContext;
import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.rsp.ReturnResult;
import com.smooth.dao.gen.bean.BizJobApplay;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.biz.applyjob.service.BizJobApplayService;
import com.smooth.site.sx.mag.biz.compcollege.comp.service.PcCollegeCompService;
import com.smooth.site.sx.pc.job.enums.ApplyJobStatus;
import com.smooth.site.sx.pc.job.req.invite.InviteApplyReq;
import com.smooth.site.sx.pc.job.service.PcInviteApplyService;
import com.smooth.site.sx.pc.job.service.PcJobService;
import com.smooth.sys.pc.vo.ListRsp;

@Controller
@RequestMapping(value = "/pc")
public class PcYStuJobInviteController {
	@Autowired
	PcCollegeCompService pcCollegeCompService;
	@Autowired
	PcJobService pcJobService;
	@Autowired
	PcInviteApplyService pcInviteApplyService;
	@RequestMapping(value = {"c/stu/queryApply"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult queryStuApply(@Valid InviteApplyReq inviteApplyReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudent = (BizStudent)PcAccountContext.getObj(BizStudent.class);
		//申请中的 等添加
		inviteApplyReq.setUid(bizStudent.getId());
		ListRsp<BizJobApplay> brsp = pcInviteApplyService.queryStuJobs(inviteApplyReq);
		returnResult.setData(brsp);
		return returnResult;
	}
	@Autowired
	BizJobApplayService bizJobApplayService;
	
	@RequestMapping(value = {"c/stu/optApply"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult optApply(@Valid InviteApplyReq inviteApplyReq,BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		BizStudent bizStudent = (BizStudent)PcAccountContext.getObj(BizStudent.class);
		//申请中的 等添加
		inviteApplyReq.setUid(bizStudent.getId());
		
		BizJobApplay bizJobApplay = bizJobApplayService.get(inviteApplyReq.getApplyId());
		if(!bizJobApplay.getStuId().equals(bizStudent.getId())) {
			throw new SiteException(SiteCodeEnums.param_error,bizJobApplay.getStuId()+ "权限错误");
		}
		
		ApplyJobStatus applyJobStatus = ApplyJobStatus.getStatus(inviteApplyReq.getApplyJobStatus());
		BizJobApplay bizJobApplayRecord = new BizJobApplay();
		bizJobApplayRecord.setId(inviteApplyReq.getApplyId());
		bizJobApplayRecord.setApplayStatus(applyJobStatus.value);
		bizJobApplayService.save(bizJobApplayRecord);
		
		returnResult.setData(bizJobApplayService.get(inviteApplyReq.getApplyId()));
		return returnResult;
	}
}
